﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WorkStation.UI.Controls
{
    public class CircleGraduation : MotionShape
    {
        public PointF CenterPoint;
        public double Radius;
        public int GraduationInterval { get; set; } = 30;
        public int GridInterval { get; set; } = 10;
        public bool IsCCW { get; set; }
        public CircleGraduation()
        {

        }
        public CircleGraduation(string description)
            : base(description)
        {

        }
        public override void Draw(Graphics g)
        {
            Pen circlepen = new Pen(PaintColor, LineWidth);
            Pen gridpen = new Pen(Color.LightGray, 2);
            gridpen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
            g.TranslateTransform(CenterPoint.X, CenterPoint.Y);
            for(int i = 0; i < 360; i += GridInterval)
            {
                g.RotateTransform(i + 90);
                //g.DrawLine(gridpen, new PointF(0, (float)(-Radius)), new PointF(0, 0));
                if (i % GraduationInterval == 0)
                {
                    g.DrawLine(circlepen, new PointF(0, (float)(-Radius)), new PointF(0, (float)(-Radius + 10)));
                    var format = new StringFormat()
                    {
                        Alignment = StringAlignment.Center,
                        LineAlignment = StringAlignment.Center,
                    };
                    if (i <= 180)
                    {
                        if(IsCCW)
                            g.DrawString($"-{i}°", new Font("微软雅黑", 9, FontStyle.Regular), new SolidBrush(Color.Black), 0, (float)(-Radius - 10), format);
                        else
                            g.DrawString($"{i}°", new Font("微软雅黑", 9, FontStyle.Regular), new SolidBrush(Color.Black), 0, (float)(-Radius - 10), format);
                    }
                    else
                    {
                        if (IsCCW)
                            g.DrawString($"{360 - i}°", new Font("微软雅黑", 9, FontStyle.Regular), new SolidBrush(Color.Black), 0, (float)(-Radius - 10), format);
                        else
                            g.DrawString($"{i - 360}°", new Font("微软雅黑", 9, FontStyle.Regular), new SolidBrush(Color.Black), 0, (float)(-Radius - 10), format);
                    }
                }
                g.RotateTransform(-i - 90);
            }
            g.TranslateTransform(-CenterPoint.X, -CenterPoint.Y);
        }
    }
}
